home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / comm / msged400.zip / src / getopts.c < prev    next >
C/C++ Source or Header  |  1996-07-23  |  5KB  |  109 lines

  1. /*********************************************************************/
  2. /*                                                                   */
  3. /*  This Program Written by Paul Edwards.                            */
  4. /*  Released to the public domain                                    */
  5. /*                                                                   */
  6. /*********************************************************************/
  7. /*********************************************************************/
  8. /*                                                                   */
  9. /*  getopts - scan the command line for switches.                    */
  10. /*                                                                   */
  11. /*  This program takes the following parameters:                     */
  12. /*                                                                   */
  13. /*  1) argc (which was given to main)                                */
  14. /*  2) argv (which was given to main)                                */
  15. /*  3) Array of options                                              */
  16. /*                                                                   */
  17. /*  Returns the number of the argument that is next to be processed  */
  18. /*  that wasn't recognised as an option.                             */
  19. /*  Example of use:                                                  */
  20. /*                                                                   */
  21. /*  #include <getopts.h>                                             */
  22. /*  int baud = 2400;                                                 */
  23. /*  char fon[13] = "telix.fon";                                      */
  24. /*  opt_t opttable[] =                                               */
  25. /*  {                                                                */
  26. /*    { "b", OPTINT, &baud },                                        */
  27. /*    { "f", OPTSTR, fon },                                          */
  28. /*    { NULL, 0, NULL }                                              */
  29. /*  }                                                                */
  30. /*  optup = getopts(argc,argv,opttable);                             */
  31. /*                                                                   */
  32. /*  The OPTINT means that an integer is being supplied.  OPTSTR      */
  33. /*  means a string (with no check for overflow).  Also there is      */
  34. /*  OPTBOOL which means it is a switch that is being passed, and an  */
  35. /*  OPTLONG to specify a long.  Also OPTFLOAT for float.             */
  36. /*                                                                   */
  37. /*  This program was inspired by a description of a getargs function */
  38. /*  written by Dr Dobbs Small-C Handbook.  Naturally I didn't get    */
  39. /*  to see the code, otherwise I wouldn't be writing this!           */
  40. /*                                                                   */
  41. /*  This program is dedicated to the public domain.  It would be     */
  42. /*  nice but not necessary if you gave me credit for it.  I would    */
  43. /*  like to thank the members of the International C Conference      */
  44. /*  (in Fidonet) for the help they gave me in writing this.          */
  45. /*                                                                   */
  46. /*  Written 16-Feb-1990.                                             */
  47. /*                                                                   */
  48. /*********************************************************************/
  49.  
  50. /*#define DOFLOAT */
  51.  
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include <errno.h>
  55. #include <stdio.h>
  56.  
  57. #include "getopts.h"
  58.  
  59. int getopts(int argc, char **argv, opt_t opttable[])
  60. {
  61.     int i, j;
  62.  
  63.     argv++;
  64.     argc--;
  65.     for (i = 1; i <= argc; i++)
  66.     {
  67.         if ((*(*argv) != '-') && (*(*argv) != '/'))
  68.             return (i);
  69.         for (j = 0; opttable[j].sw != NULL; j++)
  70.         {
  71.             if (strncmp(*argv + 1, opttable[j].sw,
  72.                         strlen(opttable[j].sw)) == 0)
  73.             {
  74.                 switch ((int)opttable[j].opttyp)
  75.                 {
  76.                 case OPTINT:
  77.                     *((int *)opttable[j].var) =
  78.                         (int)strtol(*argv + 1 + strlen(opttable[j].sw), NULL, 10);
  79.                     if (errno == ERANGE)
  80.                         return (i);
  81.                     break;
  82.                 case OPTSTR:
  83.                     strcpy((char *)opttable[j].var,
  84.                            *argv + 1 + strlen((char *)opttable[j].sw));
  85.                     break;
  86.                 case OPTBOOL:
  87.                     *((int *)opttable[j].var) = 1;
  88.                     break;
  89.                 case OPTLONG:
  90.                     *((long *)opttable[j].var) =
  91.                         strtol(*argv + 1 + strlen(opttable[j].sw), NULL, 10);
  92.                     if (errno == ERANGE)
  93.                         return (i);
  94.                     break;
  95. #ifdef DOFLOAT
  96.                 case OPTFLOAT:
  97.                     *((float *)opttable[j].var) =
  98.                         (float)strtod(*argv + 1 + strlen(opttable[j].sw), NULL);
  99.                     break;
  100. #endif
  101.                 }
  102.                 break;
  103.             }
  104.         }
  105.         argv++;
  106.     }
  107.     return (i);
  108. }
  109.